//
//  SureOrderViewController.m
//  Doctors
//
//  Created by 王森 on 16/4/21.
//  Copyright © 2016年 王森. All rights reserved.
//

#import "SureOrderViewController.h"
#import "SubmitTableViewCell.h"
#import "HospitaRetainlPriceTableViewCell.h"
#import "InformationTableViewCell.h"
#import "UIButton+Block.h"
#import "CreatOrderViewController.h"
#import "ConsumptionHistoryViewController.h"
@interface SureOrderViewController ()
{
    NSString *receiptId;
    
}
@property(nonatomic,strong)UIButton *tempButton;
@property(nonatomic,strong)NSMutableArray *temparray;

@end


@implementation SureOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addLeftBackBtn:nil];
    self.title=@"信息查询";
    _temparray=@[].mutableCopy;
    self.dataArray= self.dic[@"balanceList"];
    [_temparray addObject:@"请求到数据了"];

    [self customTableView];
    [self registerNib:@"InformationTableViewCell"];
    [self registerNib:@"HospitaRetainlPriceTableViewCell"];
    [self registerNib:@"SubmitTableViewCell"];

//    
//    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
//    [dic setObject:@"" forKey:@"medicareCard"];
//    [dic setObject:@"18810689013" forKey:@"mobile"];
//    [dic setObject:[self getArtisanId] forKey:@"artisanId"];
//    
    
//
//    [[WSNetworkApi shareManager]postWithUrl:@"/user/getUserInfo" parameters:dic option:RequestCenterCachePolicyNormal sucess:^(id responseObject) {
//        [self removeStatuslable];
//        
//        NSLog(@"%@",responseObject);
//        
//        
//        
//        if ([responseObject[@"status"] isEqualToString:@"ok"]) {
//            
//           self.dic=responseObject[@"user"];
//            
//            self.dataArray= self.dic[@"balanceList"];
//            [_temparray addObject:@"请求到数据了"];
//            
//            
//            
//        }
//        else{
//            
//            [self showFailed:responseObject[@"message"]];
//            
//        }
//        
//        [self.tableview reloadData];
//        
//        
//    } failur:^(NSError *error) {
//        [self removeStatuslable];
//        
//    }];
//
    // Do any additional setup after loading the view.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return _temparray.count;
    }
    if (section==1) {
        return self.dataArray.count;
    }else
    return _temparray.count;
}

-(NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return 249;
    }
    if (indexPath.section==1) {
        return 44;
   
        
    }
    else{
    return 70;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==0) {
        InformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InformationTableViewCell"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.name.text=[NSString stringWithFormat:@"姓名：%@",self.dic[@"username"]];
        cell.sheBao.text=[NSString stringWithFormat:@"社保卡号：%@",self.dic[@"medicareCard"]];
        cell.mobile.text=[NSString stringWithFormat:@"手机号：%@",self.dic[@"mobile"]];
        cell.jiFen.text=[NSString stringWithFormat:@"积分：%@",self.dic[@"point"]];
        cell.reTain.text=[NSString stringWithFormat:@"剩余余额：%@元",self.dic[@"balance"]];
        NSString *tempSte=nil;
        
        if ([self.dic[@"consume"] isKindOfClass:[NSNull class]]) {
           tempSte=@"0";
            
        }
        else{
            tempSte=self.dic[@"consume"];
                     
        }
        cell.lastLabel.text=[NSString stringWithFormat:@"最近消费：%@元",tempSte];
[cell.detailBtn addActionHandler:^(NSInteger tag) {
   
    ALLOC(ConsumptionHistoryViewController, view);
    view.userId=self.dic[@"userId"];
    
    [self pushNextViewController:view];
    
    NSLog(@"dsdd");
}];
        

        return cell;
        
    }
    
    if (indexPath.section==1) {
        HospitaRetainlPriceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HospitaRetainlPriceTableViewCell"];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        [cell.chooseBtn setImage:[UIImage imageNamed:@"btn_choose"] forState:UIControlStateNormal];
        [cell.chooseBtn setImage:[UIImage imageNamed:@"btn_choose_p"] forState:UIControlStateSelected];
        cell.chooseBtn.tag=indexPath.row+200;

        cell.chooseBtn=self.tempButton;
        [cell.chooseBtn addTarget:self action:@selector(chooseHospital:) forControlEvents:UIControlEventTouchUpInside];
        
        cell.content.text=[NSString stringWithFormat:@"余额：%@元",self.dataArray[indexPath.row][@"balance"]];
        cell.title.text=self.dataArray[indexPath.row][@"hospitalName"];
       NSString *str=[NSString stringWithFormat:@"%@",self.dataArray[indexPath.row][@"balance"]] ;
        NSString *temp=[NSString stringWithFormat:@"%lu",str.length];
        
        NSLog(@"长度：%@",str);
        
        
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString: cell.content.attributedText];
        [text addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(3, [temp intValue])];
        [cell.content setAttributedText: text];
        return cell;

    }
    else{
        SubmitTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SubmitTableViewCell"];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.xianIng.hidden=YES;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
[cell.submitBtn addActionHandler:^(NSInteger tag) {
    
    if (self.tempButton.enabled&&receiptId!=nil) {
        ALLOC(CreatOrderViewController, view);
        view.userId=self.dic[@"userId"];
        view.receiptId=receiptId;
        
        
        [self pushNextViewController:view];
        }
    else{
        [self showFailed:@"请选择一个医院"];
    }
  
    
    NSLog(@"dsdsdsds");
}];
        
        cell.contentView.backgroundColor=UIColorFromRGB(0xf4f4f4);
        return cell;
 
    }
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    receiptId=self.dataArray[indexPath.row][@"receiptId"];
    
    UIButton *btn = [self.view viewWithTag:indexPath.row +200];

    self.tempButton.selected=NO;
    
    btn.selected=YES;
    
    self.tempButton=btn;
    

}
-(void)chooseHospital:(UIButton *)btn
{
//    btn.enabled=!btn.enabled;
    self.tempButton.selected=NO;
    
    btn.selected=YES;
    
    self.tempButton=btn;
    

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
